<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    let wiggleSort = function (nums) {
      let sheng = nums[1] - nums[0] > 0
      let i = 1
      let j = 2
      while (j < nums.length) {
        if (nums[j] > nums[i] && sheng) {
          // 交换位置
          [nums[j], nums[i]] = [nums[i], nums[j]]
        }
        if (nums[j] < nums[i] && !sheng) {
          // 交换位置
          [nums[j], nums[i]] = [nums[i], nums[j]]
        }
        sheng = !sheng
        i++
        j++
      }
      return nums
    };
    console.log(wiggleSort([5, 3, 1, 2, 3]));
  </script>
</body>

</html>